package com.foreveross.bsl.mdm.ios;

import java.util.Date;
import java.util.UUID;

import com.foreveross.bsl.mdm.ios.cmds.ClearPasscodeCommand;
import com.foreveross.bsl.mdm.ios.cmds.DeviceInformationCommand;
import com.foreveross.bsl.mdm.ios.cmds.DeviceLockCommand;
import com.foreveross.bsl.mdm.ios.cmds.EraseDeviceCommand;
import com.foreveross.bsl.mdm.ios.cmds.InstallApplicationCommand;
import com.foreveross.bsl.mdm.ios.cmds.InstalledApplicationListCommand;
import com.foreveross.bsl.mdm.model.ios.domain.MdmTask;

/**
 * @description iosMDM任务工厂
 * 
 * @author heyaowen
 * @version v1.0
 * 
 * @2014-7-21
 * 
 */
public class MdmTaskFactory {
	
	private static void initTask(MdmTask task, String apnsToken, String commandUUID) {
		task.setApnsToken(apnsToken); // /server 查询任务条件
		task.setStatus("no"); // 未送达
		task.setCommandUUID(commandUUID); // 这次任务的UUID，设备应答需要回传的plist字段
		task.setCreate_time(new Date());
	}
	
	/**
	 *  获取设备信息任务
	 * @return
	 */
	public static MdmTask createDeviceInformationTask(String apnsToken) {
		String commandUUID = UUID.randomUUID().toString();
		MdmTask task = new MdmTask();
		task.setName("DeviceInformation");
		task.setCode("1");
		MdmTaskFactory.initTask(task, apnsToken, commandUUID);
		DeviceInformationCommand command = (DeviceInformationCommand) CommandFactory.createDeviceInformationCommand();
		command.setCommandUUID(commandUUID);
		command.execute(); // 组装plist格式 指令
		task.setCommand(command.bytes());
		return task;
	}
	
	
	/** 锁定设备
	 * @param apnsToken
	 * @param pin
	 * @param message
	 * @param phoneNumber
	 * @return
	 */
	public static MdmTask createDeviceLockCommandTask(String apnsToken, String pin, String message, String phoneNumber) {
		String commandUUID = UUID.randomUUID().toString();
		MdmTask task = new MdmTask();
		task.setName("DeviceLock");
		task.setCode("2");
		MdmTaskFactory.initTask(task, apnsToken, commandUUID);
		DeviceLockCommand command = (DeviceLockCommand) CommandFactory.createDeviceLockCommand(pin, message, phoneNumber);
		command.setCommandUUID(commandUUID);
		command.execute();
		task.setCommand(command.bytes());
		return task;
	}
	
	
	/** 清除密码
	 * @param apnsToken
	 * @param unlockToken
	 * @return
	 */
	public static MdmTask createClearPasscodeCommandTask(String apnsToken, byte[] unlockToken) {
		String commandUUID = UUID.randomUUID().toString();
		MdmTask task = new MdmTask();
		task.setName("ClearPasscode");
		task.setCode("3");
		MdmTaskFactory.initTask(task, apnsToken, commandUUID);
		ClearPasscodeCommand command = (ClearPasscodeCommand) CommandFactory.createClearPasscodeCommand(unlockToken);
		command.setCommandUUID(commandUUID);
		command.execute(); // 组装plist格式 指令
		task.setCommand(command.bytes());
		return task;
	}
	
	/** 擦除设备
	 * @param apnsToken
	 * @param pin
	 * @return
	 */
	public static MdmTask createEraseDeviceCommandTask(String apnsToken, String pin) {
		String commandUUID = UUID.randomUUID().toString();
		MdmTask task = new MdmTask();
		task.setName("EraseDevice");
		task.setCode("4");
		MdmTaskFactory.initTask(task, apnsToken, commandUUID);
		EraseDeviceCommand command = (EraseDeviceCommand) CommandFactory.createEraseDeviceCommand(pin);
		command.setCommandUUID(commandUUID);
		command.execute(); // 组装plist格式 指令
		task.setCommand(command.bytes());
		return task;
	}
	
	/** 安装列表
	 * @param apnsToken
	 * @param pin
	 * @return
	 */
	public static MdmTask createInstallApplicationListCommandTask(String apnsToken, String identifies, 
			boolean managedAppsOnly) {
		String commandUUID = UUID.randomUUID().toString();
		MdmTask task = new MdmTask();
		task.setName("InstallApplicationList");
		task.setCode("5");
		MdmTaskFactory.initTask(task, apnsToken, commandUUID);
		InstalledApplicationListCommand command = (InstalledApplicationListCommand) CommandFactory
				.createInstalledApplicationListCommand(identifies, managedAppsOnly);
		command.setCommandUUID(commandUUID);
		command.execute(); // 组装plist格式 指令
		task.setCommand(command.bytes());
		return task;
	}
	
	/** 通过iTunesStoreId安装第三方程序
	 * @param iTunesStoreID
	 * @return
	 */
	public static MdmTask createInstallApplicationByiTunesStoreIDTask(String apnsToken, String iTunesStoreID) {
		String commandUUID = UUID.randomUUID().toString();
		MdmTask task = new MdmTask();
		task.setName("InstallApplication");
		task.setCode("6");
		MdmTaskFactory.initTask(task, apnsToken, commandUUID);
		InstallApplicationCommand command = (InstallApplicationCommand) CommandFactory
				.createInstallApplicationByiTunesStoreID(iTunesStoreID);
		command.setCommandUUID(commandUUID);
		command.execute();
		task.setCommand(command.bytes());
		return task;
	}
	
	/*public static MdmTask createInstalledApplicationListMdmTask(Collection<String> identifies) {
		MdmTask MdmTask = createMdmTask();
		Command command = CommandFactory.createInstalledApplicationListCommand(identifies);
		return MdmTaskExecuteCommandWithName(MdmTask, command, "InstalledApplicationList");
	}
	protected static MdmTask MdmTaskExecuteCommandWithName(MdmTask MdmTask,
			Command command, String name) {
		MdmTask.setName(name);
		command.setCommandUUID(MdmTask.getCommandUUID());
		command.execute();
		MdmTask.setCommand(command.bytes());
		return MdmTask;
	}
	public static MdmTask createManagedApplicationListMdmTask(
			Collection<String> identifies) {
		MdmTask MdmTask = createMdmTask();
		Command command = CommandFactory.createManagedApplicationListCommand(identifies);
		return MdmTaskExecuteCommandWithName(MdmTask, command, "ManagedApplicationList");
	}

	public static MdmTask createRemoveApplicationCommandMdmTask(String identifier) {
		String commandUUID = UUID.randomUUID().toString();
		MdmTask MdmTask = new MdmTask();
		MdmTask.setStatus("no");
		MdmTask.setCommandUUID(commandUUID);
		MdmTask.setName("RemoveApplication");
		Command command = CommandFactory
				.createRemoveApplicationCommandByIdentifier(identifier);
		command.setCommandUUID(commandUUID);
		command.execute();
		MdmTask.setCommand(command.bytes());
		return MdmTask;
	}

	public static MdmTask createProfileListCommandMdmTask() {
		String commandUUID = UUID.randomUUID().toString();
		MdmTask MdmTask = new MdmTask();
		MdmTask.setStatus("no");
		MdmTask.setCommandUUID(commandUUID);
		MdmTask.setName("ProfileList");
		Command command = CommandFactory.createProfileListCommand();
		command.setCommandUUID(commandUUID);
		command.execute();
		MdmTask.setCommand(command.bytes());
		return MdmTask;
	}

	public static MdmTask createInstallProfileCommandMdmTask(byte[] bytes) {
		String commandUUID = UUID.randomUUID().toString();
		MdmTask MdmTask = new MdmTask();
		MdmTask.setStatus("no");
		MdmTask.setCommandUUID(commandUUID);
		MdmTask.setName("ProfileList");
		Command command = CommandFactory.createInstallProfileCommand(bytes);
		command.setCommandUUID(commandUUID);
		command.execute();
		MdmTask.setCommand(command.bytes());
		return MdmTask;
	}

	public static MdmTask createInstallProfileCommandMdmTask(File file) {
		String commandUUID = UUID.randomUUID().toString();
		MdmTask MdmTask = new MdmTask();
		MdmTask.setStatus("no");
		MdmTask.setCommandUUID(commandUUID);
		MdmTask.setName("InstallProfile");
		Command command = CommandFactory.createInstallProfileCommand(file);
		command.setCommandUUID(commandUUID);
		command.execute();
		MdmTask.setCommand(command.bytes());
		return MdmTask;
	}

	public static MdmTask createRemoveProfileCommandMdmTask(String identifier) {

		String commandUUID = UUID.randomUUID().toString();
		MdmTask MdmTask = new MdmTask();
		MdmTask.setStatus("no");
		MdmTask.setCommandUUID(commandUUID);
		MdmTask.setName("removeProfile");
		Command command = CommandFactory.createRemoveProfileCommand(identifier);
		command.setCommandUUID(commandUUID);
		command.execute();
		MdmTask.setCommand(command.bytes());
		return MdmTask;
	}

	public static MdmTask createRestrictionsCommandMdmTask() {

		String commandUUID = UUID.randomUUID().toString();
		MdmTask MdmTask = new MdmTask();
		MdmTask.setStatus("no");
		MdmTask.setCommandUUID(commandUUID);
		MdmTask.setName("Restrictions");
		Command command = CommandFactory.createRestrictionsCommand();
		command.setCommandUUID(commandUUID);
		command.execute();
		MdmTask.setCommand(command.bytes());
		return MdmTask;
	}

	protected static MdmTask createMdmTask() {
		MdmTask MdmTask = new MdmTask();
		MdmTask.setStatus("no");
		MdmTask.setCommandUUID(UUID.randomUUID().toString());
		return MdmTask;
	}*/
}
